package com.zeltov.eo;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.zeltov.bo.Car;
import com.zeltov.bo.Comment;


/**
 * Entity implementation class for Entity: CarEO
 * 
 */
@Entity
@Table(name = "CAR")
public class CarEO 
//extends AbstractDomainEO 
implements Car {
	
	private Date createdDate;
	
	@Version 
	private Integer version;

	public Integer getVersion() {
		return version;
	}
	public void setVersion(Integer version) {
		this.version = version;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreatedDate() { 
	    return this.createdDate; 
	}

    @Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	
	@Id
	@GeneratedValue
	@Column(name = "CAR_ID")
	private Integer id;
	private String name;
	//  @JoinTable(name = "COMMENT", joinColumns = { @JoinColumn(name = "CAR_ID") }, inverseJoinColumns = { @JoinColumn(name = "COMMENT_ID") })
	@OneToMany(orphanRemoval = true, cascade = { javax.persistence.CascadeType.ALL },targetEntity = CommentEO.class,fetch = FetchType.LAZY)
    private Set<Comment> comments = new HashSet<Comment>();
	
	private static final long serialVersionUID = 1L;
    public Set<Comment> getComments() {
        return comments;
    }
    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }
    
    @Override
	public boolean addCommentToCar(Comment comment) {
        comment.setCar(this);
        return this.getComments().add(comment);
    }

	
	/*
	private List<DriverEO> drivers = new ArrayList<DriverEO>();
	@OneToMany(mappedBy = "car", cascade = CascadeType.ALL)
    public List<DriverEO> getDriverEOs() {
        return drivers;
    }
 
    public void setDriverEOs(List<DriverEO> toys) {
        this.drivers = toys;
    }
*/	public CarEO() {
		super();
	}

	@Override
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CAR_ID_SEQ")	
	public Integer getId() {
		return this.id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

}
